<?php
/**
 * Created by PhpStorm.
 * User: zy-l
 * Date: 2019/6/19
 * Time: 1:23 AM
 */

namespace App\HttpController\Admin;

use App\HttpController\AdminBase;
use App\Model\InfoArticle;
use App\Model\InfoArticleClass;
use EasySwoole\Validate\Validate;

/**
 * 文章管理
 * Class Adv
 * @package App\HttpController\Admin
 */
class Article extends AdminBase
{

    /**
     * 文章模型
     * @return InfoArticle
     */
    public function getArticleModel()
    {
        return new InfoArticle();
    }

    /**
     * 文章分类模型
     * @return InfoArticleClass
     */
    public function getArticleClassModel()
    {
        return new InfoArticleClass();
    }


    /**
     * 首页
     * @return bool|string|void
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function index()
    {
        if ($this->isPost()) {
            $type=$this->postParam('type');
            $where=[];
            if(!empty($type)){
                $where['category_id']=$type;
            }
            $ariticModel=$this->getArticleModel();
            $data = $ariticModel->getPageAll($ariticModel->analysisWhere($where));
            $classArr=$this->getArticleClassModel()->getFiledColumnValue([],'id','name');
            foreach ($data['list'] as $key=>$val){
                $data['list'][$key]['class_name']=$classArr[$val['category_id']];
            }
            return $this->writePageJson('获取列表数据', $data);
        }
        $menu_list = $this->getArticleClassModel()->getMenuList([], 'id,name,pid as pId');
        $this->assign(['menu_list' => json_encode($menu_list)]);
        return $this->fetch('index');
    }

    /**
     * 新增
     * @return bool|string|void
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function add()
    {

        if ($this->isPost()) {
            $data = $this->postParams();
            $data['start_time'] = strtotime($this->postParam('start_time'));
            $data['end_time'] = strtotime($this->postParam('end_time'));
            return $this->writeJson('操作成功', $this->getArticleModel()->updateOrCreateData($data, ["id" => $this->postParam("id")]));
        }
        $category_id = $this->getParam('category_id');
        if (empty(intval($category_id))) {
            return   $this->showHtmlError('请先在页面左侧选择文章分类');
        } else {
            //加载模板
            $this->assign(['category_id'=>$category_id]);
            return $this->fetch();
        }
    }


    /**
     * 编辑
     * @return bool|string|void
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function edit()
    {
        $id = $this->getParam('id');
        if ($this->isPost()) {
            return $this->writeJson('操作成功', $this->getArticleModel()->updateOrCreateData($this->postParams(), ["id" => $this->postParam("id")]));
        }
        $info = $this->getArticleModel()->findOne(["id" => $id]);
        print_r($info);
        $this->assign(["data" => json_encode($info)]);
        return $this->fetch("edit");
    }

    public function del(){
        return $this->writeJson('删除成功',$this->getArticleModel()->delData(['id'=>$this->postParam('id')]));
    }

    /**
     * 提交参数验证
     * @param string|null $action
     * @return Validate|null
     */
    protected function getValidateRule(?string $action): ?Validate
    {
        $validate = null;
        return $validate;
    }
}